package com.hzw.service.impl;

import com.hzw.bean.Message;
import com.hzw.dao.MessageMapper;
import com.hzw.dto.IsReadParam;
import com.hzw.dto.MessageResponse;
import com.hzw.service.MessageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2019/4/5.
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    private MessageMapper messageMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageResponse getMessageByUserId(String userId) {
        MessageResponse messageResponse = new MessageResponse();
        //分别获取五种类型的列表
        List<Message> likeMessageList = messageMapper.getMessage(userId,0);
        List<Message> commentMessageList = messageMapper.getMessage(userId,1);
        List<Message> transpondMessageList = messageMapper.getMessage(userId,2);
        List<Message> chargeMessageList = messageMapper.getMessage(userId,3);
        List<Message> systemMessageList = messageMapper.getMessage(userId,4);

        //分别获取四种类型的未读数
        int likeNotRead = messageMapper.getNotReadNum(userId,0);
        int commentNotRead = messageMapper.getNotReadNum(userId,1);
        int transpindNotRead = messageMapper.getNotReadNum(userId,2);
        int chargeNotRead = messageMapper.getNotReadNum(userId,3);
        int systemNotRead = messageMapper.getNotReadNum(userId,4);

        messageResponse.setLikeMessageList(likeMessageList);
        messageResponse.setCommentMessageList(commentMessageList);
        messageResponse.setTranspondMessageList(transpondMessageList);
        messageResponse.setChargeMessageList(chargeMessageList);
        messageResponse.setSystemMessageList(systemMessageList);

        messageResponse.setLikeNotRead(likeNotRead);
        messageResponse.setCommentNotRead(commentNotRead);
        messageResponse.setTranspindNotRead(transpindNotRead);
        messageResponse.setChargeNotRead(chargeNotRead);
        messageResponse.setSystemNotRead(systemNotRead);

        return messageResponse;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setMessageIsRead(IsReadParam param) {
        String userId = param.getUserId();
        int type = param.getType();
        messageMapper.setMessageIsRead(userId,type);
    }
}
